package top.meijhm.communitymanagement.common.exceptions;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import top.meijhm.communitymanagement.common.error.ErrorCode;
import top.meijhm.communitymanagement.common.error.ErrorResponse;

@Slf4j
public class ApiException extends Exception {
    protected ErrorCode errorCode;
    protected String message;

    public String responseToJson() {
        ErrorResponse errorResponse = new ErrorResponse(errorCode, message);
        log.debug(errorResponse.toString());
        String response = "";
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            response = objectMapper.writeValueAsString(errorResponse);
        } catch (JsonProcessingException e) {
            log.error(e.toString());
        }
        return response;
    }

    @Override
    public String getMessage() {
        return message;
    }
}